import lib.util as util

class TagNode(object):
    
    def __init__(self, tag, attrs, data = None):
        self.tag = tag
        self.attrs = attrs
        self.data = data
        self._children = []
    
    def getChildren(self):
        return self._children
    
    def setChildren(self, children):
        self._children += children
        self.nodes = dict([(node.tag, node) for node in self._children])
    
    children = util.LateBindingProperty(getChildren, setChildren)

class ValueTag(TagNode):
      def handleChild(self, child, value):
        setattr(self,child,value)
    
class OFXTag(TagNode):
    """The master envelope tag for entire thing"""
    def __init__(self, tag, *args):
        super(OFXTag, self).__init__(tag,*args)

    
    def setChildren(self, children):
        super(OFXTag, self).setChildren(children)        
        self.status = self.nodes.get("status")
    

    
class StatusTag(ValueTag):
    """
    <STATUS><CODE>0<SEVERITY>INFO<MESSAGE>Success</STATUS>"""
    def handleChild(self, *args):
        return super(StatusTag, self).handleChild(*args)
          
class TRXUniqueID(ValueTag):
    pass
        
class BankingMessageTag(TagNode):
    """bankmsgsrsv1
      From what I can tell this acts like a list for
      all Banking account related data.
    """
    
    def __init__(self, tag, *args):
        super(BankingMessageTag, self).__init__(tag, *args)
        self.accounts = {}
    
    def setChildren(self,children):
        super(BankingMessageTag, self).setChildren(children)
        statementList = [temp for temp in children if temp is StatementTRXSet]
        debug = 1
        debug = debug * 3
        
    def __repr__(self):
        return "<BankingMessageTag - Accounts: %i >" % len(self.accounts)
        
        
class StatementTRXSet(TagNode):
    def setChildren(self, children):
        super(StatementTRXSet,self).setChildren(children)
        

class StatementTRX(ValueTag):
    def handleChild(self, child, value):
        setattr(self, child, value)